<?php
// user funcs
function runtime(){
    return round((microtime(true) - RUNTIME) * 1000, 3);
}

function fmtdate($time) {
	return date('Y-m-d H:i:s', $time + 28800);
}

function fmtsize($size, $prec=2) {
	$unit = 'b';
	$size = floatval($size);
	if($size < 0) return '';
	if($size > 1024){ $size /= 1024; $unit = 'k'; }
	if($size > 1024){ $size /= 1024; $unit = 'm'; }
	if($size > 1024){ $size /= 1024; $unit = 'g'; }
	if($size > 1024){ $size /= 1024; $unit = 't'; }
	return round($size, $prec) . $unit;
}

function cutstr($str, $len){
	return (strlen($str) > $len) ? mb_substr($str, 0, $len, 'utf-8') : $str;
}

function selected($node){
	$file = basename($_SERVER['PHP_SELF'], '.php');
	return (strstr($node, $file) ? 'class="selected"' : '');
}

// core funcs
function ht_get($key){
	return isset($_GET[$key]) ? trim($_GET[$key]) : '';
}
	
function ht_post($key){
	return isset($_POST[$key]) ? trim($_POST[$key]) : '';
}

function ht_session($key){
	return isset($_SESSION[$key]) ? trim($_SESSION[$key]) : '';
}

function ht_userip(){
	if (@$_SERVER['HTTP_CLIENT_IP'] && $_SERVER['HTTP_CLIENT_IP']!='unknown'){
		$clientip = $_SERVER['HTTP_CLIENT_IP'];
	}elseif(@$_SERVER['HTTP_X_FORWARDED_FOR'] && $_SERVER['HTTP_X_FORWARDED_FOR']!='unknown'){ 
		$clientip = $_SERVER['HTTP_X_FORWARDED_FOR'];
	}else{ 
		$clientip = $_SERVER['REMOTE_ADDR'];
	}
	return $clientip;
}

function ht_error404(){
	header('HTTP/1.1 404 Not Found');
	header('Status: 404 Not Found');
}
?>